package com.aries.crossgate.modules.sys.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aries.crossgate.common.utils.JsonDataFormate;
import com.aries.crossgate.common.utils.R;
import com.aries.crossgate.modules.sys.entity.SysRole;
import com.aries.crossgate.modules.sys.entity.SysRoleMenu;
import com.aries.crossgate.modules.sys.service.SysRoleMenuService;
import com.aries.crossgate.modules.sys.service.SysRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 系统用户管理
 * @ProjectName: crossgate
 * @Package: com.aries.crossgate.modules.sys.controller
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 李贝留斯
 * @CreateDate: 2018/5/2 15:52
 * @Version: 1.0
 **/
@Controller
@RequestMapping(value="sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    /**
     * @description: 用户列表
     * @author 李贝留斯
     * @email sunyadongsyd@163.com
     * @date 2018/5/12 21:04
     */
    @RequestMapping(value = "/list.html")
    public ModelAndView list(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("base/role/roleList");
        return modelAndView;
    }

    @RequestMapping(value = "/add.html")
    public ModelAndView add(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("base/role/roleAdd");
        return modelAndView;
    }

    @RequestMapping(value = "/edit.html")
    public ModelAndView edit(String roleId){
        ModelAndView modelAndView = new ModelAndView();
        SysRole sysRole = sysRoleService.getRoleById(roleId);
        modelAndView.setViewName("base/role/roleEdit");
        modelAndView.addObject("sysRole",sysRole);
        return modelAndView;
    }


    @RequestMapping(value = "/save.html")
    @ResponseBody
    public R save(SysRole SysRole){
        R r = sysRoleService.save(SysRole);
        return r;
    }

    @RequestMapping(value = "/delete.html")
    @ResponseBody
    public R delete(String ids){
        R r = sysRoleService.delete(ids);
        return r;
    }

    @RequestMapping(value = "/update.html")
    @ResponseBody
    public R update(SysRole SysRole){
        R r = sysRoleService.update(SysRole);
        return r;
    }

    @RequestMapping(value = "/getRoleTreeData.html")
    @ResponseBody
    public String getRoleData(){
        List<SysRole> roleList = sysRoleService.getSysRole(null,null);
        String jsonText = JSON.toJSONString(roleList, true);
        return jsonText;
    }

    @RequestMapping(value = "/getRoleData.html")
    @ResponseBody
    public JSONObject getRoleData(int page,int limit,String roleName,String roleSign){
        PageHelper.startPage(page,limit);
        List<SysRole> userList = sysRoleService.getSysRole(roleName,roleSign);
        PageInfo pageInfo = new PageInfo(userList);
        JSONObject jsonObject = JsonDataFormate.piecing(userList,pageInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/configure.html")
    @ResponseBody
    public ModelAndView configure(String roleId){
        ModelAndView modelAndView = new ModelAndView();
        List<SysRoleMenu> list = sysRoleMenuService.getRoleMenuById(roleId);
        String ids = null;
        for (SysRoleMenu sysRoleMenu : list){
            if (StringUtils.isEmpty(ids)){
                ids = String.valueOf(sysRoleMenu.getMenuId());
            }else {
                ids =ids+","+ sysRoleMenu.getMenuId();
            }
        }
        modelAndView.setViewName("base/role/roleMenuConfig");
        modelAndView.addObject("roleId", roleId);
        modelAndView.addObject("ids", ids);
        return modelAndView;
    }

    @RequestMapping(value = "/saveRoleMenu.html")
    @ResponseBody
    public R saveRoleMenu(String menuId,String roleId){
        R r = sysRoleMenuService.save(menuId,roleId);
        return r;
    }

}
